package com.cyou.nad.utils.jmx;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.cyou.nad.utils.cache.MyCache;
import com.cyou.nad.utils.cache.MyMemCache;

/**
 * 
 * @author wl
 *
 */
public class MyCacheUtils implements MyCacheUtilsMBean {
	

	
	private static final Logger log = Logger.getLogger(MyCacheUtils.class);

	private static MyMemCache getMyCache() {
		return MyMemCache.getInstance();
	}
	public static long addOrIncr(String domain, String key){
		if(StringUtils.isBlank(key)) {
			throw new IllegalArgumentException("key is null.");
		}
		long addOrIncr = getMyCache().addOrIncr(domain, key);
		if(FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			log.info("addOrIncr cache domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key );
		}
		return addOrIncr;
	}
	public static void put(String domain, String key, Object o) {
		if(StringUtils.isBlank(key)) {
			throw new IllegalArgumentException("key is null.");
		}
		getMyCache().put(domain, key, o);
		if(FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			log.info("put to cache domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key + " value=" + (null == o ? "" : o));
		}
	}

	public static void put(String domain, String key, Object o, boolean eternal, Integer timeToIdleSeconds, Integer timeToLiveSeconds, Long version) {
		if(StringUtils.isBlank(key) || null == timeToLiveSeconds) {
			throw new IllegalArgumentException("param is null.");
		}
		getMyCache().put(domain, key, o, eternal, timeToIdleSeconds, timeToLiveSeconds, version);
		if(FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			log.info("put to cache domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key + " value=" + (null == o ? "" : o) + " eternal=" + eternal + " timeToLiveSeconds=" + timeToLiveSeconds + " timeToIdleSeconds=" + (null == timeToIdleSeconds ? "" : timeToIdleSeconds));
		}
	}

	public static Object get(String domain, String key) {
		Object o = getMyCache().get(domain, key);
		if (FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			if (null != o) {
				log.info("get from cache  hit  domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key);
			} else {
				log.info("get from cache  miss  domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key);
			}
		}
		return o;
	}

	public static boolean delete(String domain, String key) {
		if(StringUtils.isBlank(key)) {
			throw new IllegalArgumentException("key is null.");
		}
		boolean f = getMyCache().delete(domain, key);
		String flag = f?"hit":"miss";
		if(FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			log.info("delete cache and cache "+flag+"  domain=" + (StringUtils.isBlank(domain) ? "" : domain) + " key=" + key);
		}
		return f;
	}
	
	public static void clearByNameSpace(String domain) {
		getMyCache().clearAll(domain);
		if(FrameWorkLogHelper.IS_CACHE_LOG_ENABLE()) {
			log.info("clear all domain cache [domain=" + (StringUtils.isBlank(domain) ? "" : domain+"]"));
		}
	
	}
	
	
	@Override
	public void clearByNameSpace(String domain,String password) {
		if(StringUtils.isBlank(password) || !"2311471259".equals(password)) {
			throw new RuntimeException("invalid password");
		}
		clearByNameSpace(domain);
	}

	@Override
	public Object getJmx(String domain, String key) {
		return get(domain, key);
	}

	@Override
	public boolean deleteJmx(String domain, String key) {
		return delete(domain, key);
	}
	public static void main(String[] args) throws InterruptedException {
		MyCacheUtils.put("ACTIVE", "active1", "active1");
		MyCacheUtils.put("ACTIVE", "active2", "active2");
		
		Thread.currentThread().sleep(500);
		System.out.println("刚刚放入cache===========");
		System.out.println(MyCacheUtils.get("ACTIVE", "active1"));
		System.out.println(MyCacheUtils.get("ACTIVE", "active2"));
		
		Thread.currentThread().sleep(500);
		MyCacheUtils.clearByNameSpace("ACTIVE");
		System.out.println("clear nameSpace ===========");
		Thread.currentThread().sleep(500);
		
		System.out.println(MyCacheUtils.get("ACTIVE", "active1"));
		System.out.println(MyCacheUtils.get("ACTIVE", "active2"));
	
		System.out.println("再次放入cache===========");
		MyCacheUtils.put("ACTIVE", "active1", "active1");
		MyCacheUtils.put("ACTIVE", "active2", "active2");
		
		Thread.currentThread().sleep(500);
		System.out.println(MyCacheUtils.get("ACTIVE", "active1"));
		System.out.println(MyCacheUtils.get("ACTIVE", "active2"));
		
	}

}
